//
//  NetworkManager.m
//  jingduoduoDemo
//
//  Created by 韩志宇 on 16/4/12.
//  Copyright © 2016年 caobo56. All rights reserved.
//

#import "NetworkManager.h"
#import "SingleTon.h"
@interface NetworkManager()
@end

@implementation NetworkManager
static NetworkManager * Manager=nil;

-(instancetype)init
{
    self=[super init];
    
    if(self)
    {
        
    }
    return self;
}
+(NetworkManager *)HttpRequestAndController:(RootViewController *)controller AndrequestAddress:(NSString * )address Andparams:(NSMutableDictionary *)params AndHUDtext:(NSString *)HUDTextt CompletionHandler:(JDDResponseBlock)response errorHandler:(JDDNKResponseErrorBlock)errorblock
{
    if (Manager==nil)
        Manager=[[super allocWithZone:NULL]init];
    MKNetworkEngine *  engine=[[SingleTon sharedInstance]engine];
    MKNetworkOperation *op = [engine operationWithPath:address  params:params httpMethod:@"POST" ssl:YES];
//    if(HUDTextt!=nil)
//        [LCProgressHUD showLoading:HUDTextt];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:[[completedOperation responseString]dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
        [[dic objectForKey:@"code"]intValue]==1?response([[dic objectForKey:@"data"]copy]):[LCProgressHUD showFailure:[dic objectForKey:@"message"]];
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        NSString  *str=@"网络似乎不太给力,请稍后再试";
        if (errorblock)
            errorblock([str copy]);
        //        [LCProgressHUD showFailure:@"网络似乎不太给力,请稍后再试"];
    }];
    
    [engine enqueueOperation:op];
    return Manager;
    
    
}
@end
